#include "gtcell.h"

void GtCell::setElectric(quint32 value) noexcept
{
    if (value < 0 || value > 100)
        return;
    m_electric = value;
    emit electricChanged();
    update();
}

void GtCell::setColor(QColor color) noexcept
{
    m_color = color;
    emit colorChanged();
    update();
}

void GtCell::setThreshold(quint32 value) noexcept
{
    if (value < 0 || value > 100)
        return;
    m_threshold = value;
    emit thresholdChanged();
    update();
}

quint32 GtCell::getElectric() const noexcept
{
    return m_electric;
}

QColor GtCell::getColor() const noexcept
{
    return m_color;
}

quint32 GtCell::getThreshold() const noexcept
{
    return m_threshold;
}

void GtCell::paint(QPainter *painter)
{
    QPen pen;
    pen.setColor(Qt::darkRed);
    pen.setWidth(m_lineWidth);

    painter->setPen(pen);

    //电池头
    quint32 headWidth = width() * 0.1;  //200*0.1
    quint32 headHeight = height() * 0.3;  //80*0.3
    qDebug() << headWidth << " " << headHeight << pen.width();

    //电池的宽高，控件宽减去笔的粗细减去电池头的宽  pen.width = 1;
    //200-1-20 = 179        80-1 = 79
    auto cellWidth = width() - pen.width() - headWidth;
    auto cellHeight = height() - pen.width();

    //电池
    //0,0,179,79
    painter->drawRect(0, 0, cellWidth, cellHeight);

    auto electricWidth = (cellWidth - pen.width()) * m_electric / 100;
    auto electricHeight = cellHeight - pen.width();
    painter->fillRect(pen.width(), pen.width(), electricWidth, electricHeight, m_electric > m_threshold ? m_color : m_waringColor);

    //头
    auto headX = cellWidth + pen.width();
    auto headY = height() / 2 - headHeight / 2;
    painter->fillRect(headX, headY, headWidth, headHeight, Qt::black);
}
